import uvicorn
from fastapi import FastAPI, Depends
from starlette.middleware.cors import CORSMiddleware

from example.hook import lifespan
from example.middleware.auth import AuthMiddleware
from example.core.router import init_routers
from example.globals import app_config
from example.dependencies import token_header

app = FastAPI(dependencies=[Depends(token_header)], lifespan=lifespan)

# 跨域
app.add_middleware(
    CORSMiddleware,
    allow_origins="*",
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
# 权限校验
app.add_middleware(AuthMiddleware)

# 业务路由添加
init_routers(app)

if __name__ == '__main__':
    port = app_config.server.port
    uvicorn.run(app, host='0.0.0.0', port=port)
